﻿using Caliburn.Micro;
using SS.WPF.Framework.Localization;

namespace SS.WPF.Framework.Restriction
{
    using System;
    using System.Linq;

    public class StringEnumRestriction : EnumRestriction<string>
    {
        public override bool Validate(string value)
        {
            try
            {
                var idx = Enum.GetNames(EnumType).ToList().IndexOf(value);
                if (idx < 0)
                {
                    return false;
                }

                Enum.ToObject(EnumType, idx);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public override string Description
        {
            get
            {
                return string.Format("[{0}]", string.Join(", ",
                                                          Enum.GetValues(EnumType).Cast<object>().Select(
                                                              x => IoC.Get<TranslationManager>().Translate(x.ToString()))));
            }
        }
    }
}